<template>
  <div class="box-list-container">
    <div class="box-query-form">
      <el-form :model="queryParam" label-width="auto" label-position="right" :gutter="24">
        <el-row>
          <el-col :xl="6" :lg="6" :md="12" :sm="24">
            <el-form-item label="所属年月:">
              <el-date-picker
                v-model="queryParam.yearMonth"
                type="month"
                value-format="yyyy-MM"
                placeholder="选择月"
              />
            </el-form-item>
          </el-col>
          <!-- <el-col :xl="6" :lg="6" :md="12" :sm="24">
            <el-form-item label="任意列数据:">
              <el-input v-model="queryParam.data" placeholder="请输入" />
            </el-form-item>
          </el-col> -->
          <el-col v-for="(item, index) in colNameArr20" :key="index" :xl="6" :lg="6" :md="12" :sm="24">
            <el-form-item :label="`列${index + 1}(${item.label})`">
              <el-input v-model="queryParam[item.prop]" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :xl="6" :lg="6" :md="12" :sm="24" class="table-search-btn">
            <el-button icon="el-icon-refresh" @click="searchReset">重置</el-button>
            <el-button type="primary" icon="el-icon-search" @click="searchQuery">查询</el-button>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div class="box-table">
      <div class="table-title">
        <div class="table-title-left">
          新表列表
        </div>
        <div>
          <el-button plain class="btnAdd" icon="el-icon-plus" @click="handleAdd('新增')">新增</el-button>
          <el-button type="primary" icon="el-icon-edit" @click="editColName">修改列名</el-button>
          <el-button type="success" icon="el-icon-setting" @click="openExport">导出设置</el-button>
        </div>
      </div>
      <!-- 批量操作 -->
      <div v-if="selectedRowList.length" class="batchAction">
        <div class="batchAction-left">
          <el-dropdown>
            <el-button>
              批量操作 <i class="el-icon-arrow-down el-icon--right" />
            </el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item>
                <div @click="batchDelete"><i class="el-icon-delete" />删除</div>
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </div>
        <div class="batchAction-right">
          已选择
          <span style="font-weight: 700">
            <el-link type="primary" :underline="false">{{ selectedRowList.length }}</el-link>
          </span>项
          <el-link type="primary" :underline="false" style="margin-left: 24px;" @click="onClearSelected">清空</el-link>
        </div>
      </div>
      <el-table ref="table" :data="dataSource" @selection-change="onSelectChange">
        <el-table-column type="selection" width="55" />
        <el-table-column fixed="left" prop="id" label="ID" width="60" />
        <!-- <el-table-column label="所属年月" prop="yearMonth" width="90" /> -->
        <el-table-column v-for="(item,index) in colNameArr21" :key="index" :prop="item.prop" :label="item.label" min-width="120" />
        <el-table-column label="创建时间" prop="createTime" width="170" />
        <el-table-column label="更新时间" prop="updateTime" width="170" />
        <el-table-column fixed="right" label="操作" width="100">
          <template slot-scope="scope">
            <el-button type="text" @click="handleEdit('编辑', scope.row)">编辑</el-button>
            <el-popconfirm title="确定删除吗？" style="margin-left: 10px" @confirm="handleDelete(scope.row[guidFieldName])">
              <el-button slot="reference" type="text" style="color: red;">删除</el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <div class="box-paging">
        <Pagination
          v-show="ipagination.total > 0"
          layout="total, sizes, prev, pager, next"
          :total="ipagination.total"
          :page.sync="ipagination.pageNum"
          :limit.sync="ipagination.pageSize"
          @pagination="loadData"
        />
      </div>
    </div>
    <handleDrawer ref="modalForm" @ok="modalFormOk" />
    <updateModelCol ref="updateModelCol" @ok="getColName" />
    <handleExport ref="handleExport" />
  </div>
</template>

<script>
import { listMixin } from '@/mixins/listMixin'
import Pagination from '@/components/Pagination'
import handleDrawer from './components/handleDrawer'
import { postAction } from '@/api/manage'
import { getColNameSortArr } from '@/utils/index'
import updateModelCol from './components/updateModelCol'
import handleExport from './components/handleExport'

export default {
  components: {
    Pagination,
    handleDrawer,
    updateModelCol,
    handleExport
  },
  mixins: [listMixin],
  data() {
    return {
      guidFieldName: 'id',
      ipagination: {
        pageNum: 1,
        pageSize: 100,
        total: 0
      },
      tConfig: {},
      queryParam: {
        yearMonth: this.$moment().format('YYYY-MM')
      },
      colNameArrBit: [], // 部分列
      colNameArr20: [],
      colNameArr21: [],
      toggleSearchStatus: false,
      userInfo: this.$ls.get('USER_INFO') || {},
      url: {
        list: 'newTable.aspx?action=GetList',
        delete: 'newTable.aspx?action=Delete',
        deleteBatch: 'newTable.aspx?action=DeleteBatch',
        colNameList: 'generalConfigList.aspx?action=GetConfig'
      }
    }
  },
  created() {},
  mounted() {
    this.getColName()
  },
  methods: {
    // 重置
    searchReset() {
      this.queryParam = {
        yearMonth: this.$moment().format('YYYY-MM')
      }
      this.loadData(1)
    },
    openExport() {
      this.$refs.handleExport.show()
      this.$refs.handleExport.title = '导出设置'
      this.$refs.handleExport.colNameArr21 = this.colNameArr21
      const param = { ...this.queryParam, ...this.ipagination }
      console.log('param', param)
      this.$refs.handleExport.queryParam = param
    },
    // 获取列名
    getColName() {
      postAction('generalConfigList.aspx?action=GetConfig', { cName: '新表列名字典' }).then((res) => {
        if (res.code === 1) {
          this.tConfig = res.data
          const data = res.data.cValue || {}
          this.colNameArr20 = getColNameSortArr(JSON.parse(data), 20)
          this.colNameArr21 = [...this.colNameArr20]
          this.colNameArr21.unshift({ prop: 'yearMonth', label: '所属年月' })
        } else {
          this.$message.warning(res.message || '操作失败！')
        }
      })
    },
    // 修改列名
    editColName() {
      this.$refs.updateModelCol.edit(this.colNameArr20, this.tConfig)
      this.$refs.updateModelCol.title = '修改列名'
    },
    // 参数设置
    parameterSet(rowData) {
      this.$refs.parameterSet.show(rowData.id)
      this.$refs.parameterSet.title = '参数设置'
    }
  }
}
</script>

<style lang="scss" scoped>
</style>

